<?

class helper_html
{
	public static $meta = array();
	public static $client_context = array();
	public static $class = '';
	
	protected static $custom_headers = array();
	protected static $js = array();

	public static function add_header( $header )
	{
		self::$custom_headers[] = $header;
	}

	public static function css( $css, $media = 'screen, projection' )
	{
		$css .= '.css';
		return '<link rel="stylesheet" href="' . self::static_host() . '/' . $css . self::get_hash($css) . '" type="text/css" media="' . $media . '">';
	}

	public static function js( $js )
	{
		$js .= '.js';
		return '<script type="text/javascript" src="' . self::static_host() . '/' . $js . self::get_hash($js) . '"></script>';
	}

	public static function get_hash( $name )
	{
		static $hashes;
		if ( !$hashes ) $hashes = include PROJECT_PATH . '/apps/main/web/public/hashes.php';

		return '?' . $hashes[$name];
	}

	public static function static_host()
	{
		$host = config::get('static_host', '/public');

		if ( is_array($host) ) $host = $host[array_rand($host)];

		return $host;
	}

	public static function add_js_context( $name, $value )
	{
		if ( $value instanceof MongoId ) $value = (string)$value;
		self::$client_context[$name] = $value;
	}

	public static function add_js_callback( $code )
	{
		self::$js[] = $code;
	}

	public static function js_init()
	{
		echo helper_html::js('system');
		if ( config::get('debug') ) echo helper_html::js('debug');

		if ( self::$client_context )
		{
			foreach ( self::$client_context as $name => $value )
			$user_context[] = "'{$name}': " . json_encode($value);
			$user_context = implode(',', $user_context);
		}

		return '<script type="text/javascript">App.init({
				action: "' . context::get('action')->get_name() . '",
				static_host: "' . self::static_host() . '",
				host: "' . $_SERVER['SERVER_NAME'] . '"
				' . ( $user_context ? ',' . $user_context : '' ) . '
			});' . ( self::$js ? implode(';', self::$js) : '' ) . '</script>';
	}

	public static function get_meta()
	{
		$title = self::$meta['title'];
		if ( self::$meta['page_title'] ) $title = self::$meta['page_title'] . ' : ' . $title;

		return
			'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' .
			'<title>' . htmlspecialchars(strip_tags($title)) . '</title>' .
			'<meta name="description" content="' . htmlspecialchars(self::$meta['description']) . '">' .
			implode('', self::$custom_headers);
	}

	public static function params( $params )
	{
		$d = '';
		foreach ( $params as $k => $v ) $d[] = $k . '="' . $v . '"';
		return $params ? implode(' ', $d) : '';
	}

	public static function image( $src, $params = array() )
	{
		$params['src'] = self::static_host() . '/images/' . $src;
		return '<img ' . self::params($params) . '/>';
	}

	public static function checkboxes($name, $values, $checked = null)
	{
		foreach ( $values as $v => $t )
			$html .= '<input type="checkbox" name="' . $name . '[]" value="' . $v . '" ' . ( @in_array($v, $checked) ? 'checked' : '') . ' /> ' . _($t) . '<br />';

		return $html;
	}
	
	public static function radios($name, $values, $checked = null)
	{
		foreach ( $values as $v => $t )
			$html .= '<input type="radio" name="' . $name . '" value="' . $v . '" ' . ($v == $checked ? 'checked' : '') . ' /> ' . _($t) . ' ';

		return $html;
	}

	public static function select($name, $values, $selected = null, $options = array())
	{
		$options['params']['name'] = $name;
		$html = '<select ' . helper_html::params($options['params']) . '>';

		if ( $options['default'] ) array_unshift ($values, $options['default']);

		foreach ( $values as $v )
		{
			if (is_array($v) )
			{
				$val = $v['_id'];
				$title = _field($v, 'title');
			}
			else
			{
				$val = $title = $v;
			}

			$html .= '<option ' . ( $val == $selected ? 'selected' : '' ) . ' value="' . htmlspecialchars($val) . '">' . htmlspecialchars($title) . '</option>';
		}

		$html .= '</select>';

		return $html;
	}

	public static function select_keys($name, $values, $selected = null, $options = array())
	{
		$options['params']['name'] = $name;
		$html = '<select ' . helper_html::params($options['params']) . '>';

		if ( $options['default'] ) $html .= '<option value="">' . $options['default'] . '</option>';

		foreach ( $values as $k => $v )
			$html .= '<option ' . ( $k == $selected ? 'selected' : '' ) . ' value="' . $k . '">' . htmlspecialchars($v) . '</option>';

		$html .= '</select>';

		return $html;
	}
}